package com.dingwei.superiordiary.ui.fragment;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.dingwei.superiordiary.R;
import com.dingwei.superiordiary.adapter.adapter.SelectedListAdapter;
import com.dingwei.superiordiary.app.MyApplication;
import com.dingwei.superiordiary.bean.Index_Bean;
import com.dingwei.superiordiary.bean.ProductsBean;
import com.dingwei.superiordiary.mvp.presenter.SelectedPresenter;
import com.dingwei.superiordiary.mvp.view.Contract;
import com.dingwei.superiordiary.ui.activity.CommodityDetailsActivity;
import com.dingwei.superiordiary.ui.activity.NewSuperiorProductsActivity;
import com.dingwei.superiordiary.ui.activity.ScanActivity;
import com.dingwei.superiordiary.ui.activity.SearchEditActivity;
import com.dingwei.superiordiary.ui.activity.WebViewActivity;
import com.jude.easyrecyclerview.EasyRecyclerView;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.lihao.baselibrary.base.BaseActivity;
import com.lihao.baselibrary.base.LazyLoadFragment;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;
import com.zhouwei.mzbanner.MZBannerView;
import com.zhouwei.mzbanner.holder.MZHolderCreator;
import com.zhouwei.mzbanner.holder.MZViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by lihao on 2018/1/4.
 * 首页-精选
 */

public class SelectedFragment extends LazyLoadFragment implements Contract.SelectedView {
    SelectedPresenter selectedPresenter = new SelectedPresenter(this);
    @Bind(R.id.img_scan)
    ImageView imgScan;
    @Bind(R.id.ll_search)
    LinearLayout llSearch;
    @Bind(R.id.banner)
    MZBannerView banner;
    @Bind(R.id.img_cold_play)
    ImageView imgColdPlay;
    @Bind(R.id.img_stall)
    ImageView imgStall;
    @Bind(R.id.img_delicacy)
    ImageView imgDelicacy;
    @Bind(R.id.m_recyclerView)
    EasyRecyclerView mRecyclerView;
    @Bind(R.id.no_data_layout)
    RelativeLayout noDataLayout;
    @Bind(R.id.data_layout)
    NestedScrollView dataLayout;
    @Bind(R.id.refresh_layout)
    SmartRefreshLayout refreshLayout;
    @Bind(R.id.no_net_btn)
    TextView noNetBtn;
    @Bind(R.id.no_net_layout)
    RelativeLayout noNetLayout;
    @Bind(R.id.img_loading)
    ImageView imgLoading;
    @Bind(R.id.fake_status_bar)
    View fakeStatusBar;
    @Bind(R.id.search_layout)
    LinearLayout searchLayout;

    private SelectedListAdapter mAdapter;
    private int page=1;
    List<Index_Bean.DataBean.AdvertBean> list =  new ArrayList<>();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    public void onResume() {
        super.onResume();
        banner.start();//开始轮播
    }

    @Override
    public void onPause() {
        super.onPause();
        banner.pause();//暂停轮播
    }

    @Override
    public void onStop() {
        super.onStop();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    protected int setContentView() {
        return R.layout.fragment_selectd;
    }

    @Override
    protected void setBindView() {
        ButterKnife.bind(this, view);
        initView();
    }


    private void initView() {

        final AnimationDrawable animDrawable = (AnimationDrawable) imgLoading
                .getBackground();
        imgLoading.post(new Runnable() {
            @Override
            public void run() {
                animDrawable.start();
            }
        });

        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.getRecyclerView().setNestedScrollingEnabled(false);
        mAdapter = new SelectedListAdapter(BaseActivity.context);
        mRecyclerView.setAdapter(mAdapter);

        refreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                page = page+ 1;
                selectedPresenter.getProducts(false,false,page);
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = 1;
                selectedPresenter.initData();

            }
        });


        mAdapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                intent  = new Intent(BaseActivity.context, CommodityDetailsActivity.class);
                Index_Bean.DataBean.ProductBean bean = mAdapter.getItem(position);
                intent.putExtra("id",bean.id);
                startActivity(intent);
            }
        });

        banner.setBannerPageClickListener(new MZBannerView.BannerPageClickListener() {
            @Override
            public void onPageClick(View view, int i) {
                Index_Bean.DataBean.AdvertBean imgListBean =  list.get(i);
                if (imgListBean.type.equals("1")){
                    intent = new Intent(BaseActivity.context,WebViewActivity.class);
                    intent.putExtra("url",imgListBean.url);
                    intent.putExtra("title","详情");
                    startActivity(intent);
                }else {
                    intent = new Intent(BaseActivity.context,CommodityDetailsActivity.class);
                    intent.putExtra("id",imgListBean.product_id);
                    startActivity(intent);
                }

            }
        });

    }

    @Override
    protected void lazyLoad() {
        selectedPresenter.initData();
    }

    @OnClick({R.id.img_scan, R.id.img_cold_play, R.id.img_stall,
            R.id.img_delicacy, R.id.no_net_btn,R.id.search_layout})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_scan:
                intent = new Intent(BaseActivity.context, ScanActivity.class);
                startActivity(intent);
                break;
            case R.id.img_cold_play:
                intent = new Intent(BaseActivity.context, NewSuperiorProductsActivity.class);
                intent.putExtra("activity_title", "服饰");
                intent.putExtra("category_id", "4");
                startActivity(intent);
                break;
            case R.id.img_stall:
                intent = new Intent(BaseActivity.context, NewSuperiorProductsActivity.class);
                intent.putExtra("activity_title", "手办");
                intent.putExtra("category_id", "3");
                startActivity(intent);
                break;
            case R.id.img_delicacy:
                intent = new Intent(BaseActivity.context, NewSuperiorProductsActivity.class);
                intent.putExtra("activity_title", "杂货");
                intent.putExtra("category_id", "2");
                startActivity(intent);
                break;
            case R.id.search_layout:
                intent = new Intent(BaseActivity.context, SearchEditActivity.class);
                startActivity(intent);
                break;
            case R.id.no_net_btn:
                selectedPresenter.initData();
                break;
        }
    }


    @Override
    public void initData(Index_Bean bean) {
        list.addAll(bean.data.advert);
        list.addAll(bean.data.advert);
        list.addAll(bean.data.advert);
        banner.setIndicatorVisible(false);
        // 设置数据
        banner.setPages(list, new MZHolderCreator<BannerViewHolder>() {
            @Override
            public BannerViewHolder createViewHolder() {
                return new BannerViewHolder();
            }
        });
        banner.start();

        mAdapter.clear();
        mAdapter.addAll(bean.data.product);

        refreshLayout.finishRefresh();
        showData("success");
    }

    private void showData(String state) {
        if (state.equals("success")){
            dataLayout.setVisibility(View.VISIBLE);
            noNetLayout.setVisibility(View.GONE);
            if (mAdapter.getAllData()!=null&&mAdapter.getAllData().size()>0){
                dataLayout.setVisibility(View.VISIBLE);
                noDataLayout.setVisibility(View.GONE);
            }else {
                dataLayout.setVisibility(View.GONE);
                noDataLayout.setVisibility(View.VISIBLE);
            }
        }else {
            dataLayout.setVisibility(View.GONE);
            noNetLayout.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void OnFails() {
        showData("fails");
    }


    @Override
    public void OnLoadMore(ProductsBean bean) {
        mAdapter.addAll(bean.data);
        refreshLayout.finishLoadmore();
        showData("success");
    }

    @Override
    public void OnRefresh(ProductsBean bean) {
        mAdapter.clear();
        mAdapter.addAll(bean.data);
        refreshLayout.finishRefresh();
        showData("success");
    }

    public static class BannerViewHolder implements MZViewHolder<Index_Bean.DataBean.AdvertBean> {
        private ImageView mImageView;
        @Override
        public View createView(Context context) {
            // 返回页面布局
            View view = LayoutInflater.from(context).inflate(R.layout.item_index_banner,null);
            mImageView = (ImageView) view.findViewById(R.id.img);
            return view;
        }

        @Override
        public void onBind(Context context, int position, Index_Bean.DataBean.AdvertBean data) {
            // 数据绑定
            Glide.with(BaseActivity.context)
                    .load(data.icon)
                    .apply(MyApplication.options)
                    .into(mImageView);

        }
    }
}
